/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef WIFI_LOCK_MANAGER_H
#define WIFI_LOCK_MANAGER_H

#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <mutex>
#include "wifi_logger.h"
#include "wifi_errcode.h"
#include "application_state_observer_stub.h"
#include "app_state_data.h"
#include "ipc_skeleton.h"
#include "event_handler.h"
#include "event_runner.h"
#include "wifi_msg.h"

namespace OHOS {
namespace Wifi {
enum WifiMode {
    WIFI_MODE_FULL_HIGH_PERF = 0,
    WIFI_MODE_NO_LOCKS_HELD = 1,
};

class WifiLockManagerBase {
public:
    WifiLockManagerBase();
    virtual ~WifiLockManagerBase();
    /**
     * Acquire wifi lock by uid
     * @param callingUid calling bundle uid
     * @return true if acquire success else return false;
     */
    ErrCode AcquireWifiLock(pid_t callingUid);
    /**
     * Release wifi lock by uid
     * @param callingUid calling bundle uid
     * @return true if release success else return false;
     */
    ErrCode ReleaseWifiLock(pid_t callingUid);
    /**
     * Update the mode of wifi
     * @return true if update success else return false;
     */
    bool UpdateWifiMode();
    /**
     * Remove app uid from wifi lock infos
     * @param uid bundle uid
     */
    void RemoveLockForAppDie(pid_t uid);
    /**
     * Get wifi connect state
     * @return ConnState;
     */
    ConnState GetWifiConnectState();
    /**
     * Set wifi to new mode
     * @param newLockMode wifi mode
     * @return true if set success else return false;
     */
    virtual bool SetNewMode(WifiMode newLockMode);
    /**
     * Get wifi lock mode
     * @return WifiMode;
     */
    virtual WifiMode GetLockMode();

    virtual bool ResetCurrentMode();
public:
    WifiMode currentWifiMode_ = WIFI_MODE_FULL_HIGH_PERF;
    uint64_t lastLockTimeStamp_;
    uint32_t refCount_ = 0;
    std::set<pid_t> wifiLockBundles_;
    std::mutex mutex_;
};

class AppStateObserver : public AppExecFwk::ApplicationStateObserverStub {
public:
    /**
     * Application foreground state changed callback.
     *
     * @param appStateData Application Process data.
     */
    //virtual void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override;

    /**
     * Will be called when the ability state changes.
     *
     * @param abilityStateData Ability state data.
     */
    //virtual void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override;

    /**
     * Will be called when the extension state changes.
     *
     * @param abilityStateData Extension state data.
     */
    //virtual void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) override;

    /**
     * Will be called when the process start.
     *
     * @param processData Process data.
     */
    virtual void OnProcessCreated(const AppExecFwk::ProcessData &processData) override;

    /**
     * Will be called when the process die.
     *
     * @param processData Process data.
     */
    virtual void OnProcessDied(const AppExecFwk::ProcessData &processData) override;
    /**
     * Will be called when the application start.
     *
     * @param appStateData Application state data.
     */
    virtual void OnAppStarted(const AppExecFwk::AppStateData &appStateData) override;

    /**
     * Will be called when the application stop.
     *
     * @param appStateData Application state data.
     */
    virtual void OnAppStopped(const AppExecFwk::AppStateData &appStateData) override;
};

class WifiLockManager : public WifiLockManagerBase {
public:
    WifiLockManager();
    virtual ~WifiLockManager();
    static WifiLockManager* GetInstance();
    void RegisterAppStateObserver();
    virtual bool SetNewMode(WifiMode newLockMode) override;
    virtual WifiMode GetLockMode() override;
    virtual bool ResetCurrentMode() override;
private:
    static WifiLockManager* g_instance;
    static std::mutex insMutex_;
    std::shared_ptr<AppExecFwk::EventRunner> appChangeEventRunner_ = nullptr;
    std::shared_ptr<AppExecFwk::EventHandler> appChangeEventHandler_ = nullptr;
    sptr<AppStateObserver> appStateObserver_;
};
} // namespace Wifi
} // namespace OHOS
#endif // WIFI_LOCK_MANAGER_H
